/*
* Copyright (C) 2012 Manuel Pérez
*
* This file is part of OpenSudoku.
*
* OpenSudoku is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenSudoku is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenSudoku. If not, see <http://www.gnu.org/licenses/>.
*
*/
package igraf.login;

import igraf.Igraf;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

import dBasea.Logina;
import email.EmailSendService;
import java.awt.Color;

public class Login extends JFrame {
	private static final long serialVersionUID = 720031532933871989L;

	private Logina bd= new Logina();
	private JTextField ize;
	private JPasswordField pass;
	private JLabel la;
	private JLabel le;
	private JButton ok;

	public Login() {
		
		
		// Panel bat ,argazki batekin eta maximizatzen denean ondo geratzen
		// dena, gainera sartu daitezke gauzak bere gainean

		this.setTitle("Login");
		this.setBounds(250, 350, 900, 600);
		this.setResizable(true);
		this.setLocationRelativeTo(null);

		ArgazkiPanel mp = new ArgazkiPanel(200, 300);

		getContentPane().setLayout(new BorderLayout(0, 0));
		getContentPane().add(mp, BorderLayout.CENTER);

		// Sartuko ditut guztiak beste panel batean

		JPanel goikoa = new JPanel();
		goikoa.setBackground(new Color(102, 153, 204));

		JButton button = new JButton("Ahaztu duzu pasahitza?");
		goikoa.add(button);
		button.addActionListener(new ActionListener() {


			public void actionPerformed(ActionEvent arg0) {
				try {
					String emaila = JOptionPane
							.showInputDialog("Sartu zure email:");

					// begiratu ea zerbait idatzi den emailean
					if (emaila == null || emaila.length() > 0) {

						// begiratu ea balio den emaila eta ona bada bidali pass
						if (bd.balioEmail(emaila)) {
							String pass = bd.lortuMail(emaila);
							EmailSendService.getInstance()
									.sendEmail("Pasahitza gogoratzeko email",
											pass, emaila);
						} else {
							JOptionPane
									.showMessageDialog(
											null,
											"¡Ez da zuzena!.Begiratu ondo jarri duzun\n"
													+ "zure emaila edo ziurtatu konexioa dagoela",
											"Oker", JOptionPane.ERROR_MESSAGE);
						}
					} else {
						JOptionPane.showMessageDialog(null,
								"Sartu behar duzu emaila\n"
										+ "Ezin da eremu hutsik egon", "Oker",
								JOptionPane.ERROR_MESSAGE);
					}
				} catch (Exception e) {
					JOptionPane.showMessageDialog(null,"Sartu behar duzu emaila ondo\n"
					+ "Ezin da eremu hutsik egon", "Oker",JOptionPane.ERROR_MESSAGE);
					e.printStackTrace();
				}
			}
		});

		goikoa.setLayout(new FlowLayout(FlowLayout.RIGHT));

		la = new JLabel();
		la.setText("Sartu zure izena");
		la.setSize(100, 10);
		goikoa.add(la);

		ize = new JTextField();
		ize.setColumns(10);
		ize.setSize(100, 10);
		goikoa.add(ize);

		le = new JLabel();
		le.setText("Sartu zure password");
		le.setSize(100, 10);
		goikoa.add(le);

		pass = new JPasswordField();
		pass.setColumns(10);
		pass.setSize(100, 10);
		goikoa.add(pass);

		ok = new JButton();
		ok.setText("Baieztatu");
		ok.setSize(100, 20);
		goikoa.add(ok);

		getContentPane().add(goikoa, BorderLayout.NORTH);

		// Baieztatu botoiari jarri akzio bat
		ActionListener sakatu = new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				try {
					// begiratu ea zerbait idatzi duen
					if (ize.getText().length() > 0
							&& pass.getPassword().length > 0) {
						// ondo sartu bada
						if (bd.balidatuErabiltzailea(ize.getText(), new String(
								pass.getPassword()))) // balidatu metodoa deitu
						{
							// Hasierako Pantailak ikustarazten duena
							// setVisible(false);

							Igraf hasiera = new Igraf();
							hasiera.setVisible(true);
							Login.this.setVisible(false);
						} else {
							JOptionPane.showMessageDialog(null,
									"Erabiltzaile pasahitza ez dira egokiak.",
									"Oker", JOptionPane.ERROR_MESSAGE);

							ize.requestFocusInWindow();
						}

					} else {
						JOptionPane.showMessageDialog(null,
								"Sartu behar duzu erabiltzaile eta pasahitza.\n"
										+ "Ezin da eremu hutsik egon", "Oker",
								JOptionPane.ERROR_MESSAGE);
					}

				} catch (Exception e) {
					e.printStackTrace();
				}

			}
		};
		ok.addActionListener(sakatu);

	}


	public static void main(String[] args) {
		Login main = new Login();
		main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		main.setVisible(true);

	}
}
